home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / minibuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  22.7 KB  |  834 lines

  1. /* Minibuffer input and completion.
  2.    Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Copyright (C) 1994, 1995 Amdahl Corporation.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: Mule 2.0, FSF 19.28.  Mule-ized except as noted.
  22.    Substantially different from FSF. */
  23.  
  24. #include <config.h>
  25. #include "lisp.h"
  26.  
  27. #include "buffer.h"
  28. #include "commands.h"
  29. #include "device-stream.h"
  30. #include "frame.h"
  31. #include "insdel.h"
  32. #include "redisplay.h"
  33. #include "window.h"
  34.  
  35. /* Depth in minibuffer invocations.  */
  36. int minibuf_level;
  37.  
  38. Lisp_Object Qcompletion_ignore_case;
  39.  
  40. /* Nonzero means completion ignores case.  */
  41. int completion_ignore_case;
  42.  
  43. /* List of regexps that should restrict possible completions.  */
  44. Lisp_Object Vcompletion_regexp_list;
  45.  
  46. /* The echo area buffer. */
  47. Lisp_Object Vecho_area_buffer;
  48.  
  49. /* Prompt to display in front of the minibuffer contents */
  50. Lisp_Object Vminibuf_prompt;
  51.  
  52. /* Hook to run just after entry to minibuffer. */
  53. Lisp_Object Qminibuffer_setup_hook, Vminibuffer_setup_hook;
  54.  
  55. Lisp_Object Qappend_message, Qcurrent_message_label,
  56.             Qclear_message, Qdisplay_message;
  57.  
  58.  
  59. DEFUN ("minibuffer-depth", Fminibuffer_depth, Sminibuffer_depth, 0, 0, 0,
  60.   "Return current depth of activations of minibuffer, a nonnegative integer.")
  61.   ()
  62. {
  63.   return make_number (minibuf_level);
  64. }
  65.  
  66. /* The default buffer to use as the window-buffer of minibuffer windows */
  67. /*  Note there is special code in kill-buffer to make this unkillable */
  68. Lisp_Object Vminibuffer_zero;
  69.  
  70.  
  71. /* Actual minibuffer invocation. */
  72.  
  73. static Lisp_Object
  74. read_minibuffer_internal_unwind (Lisp_Object unwind_data)
  75. {
  76.   Lisp_Object frame;
  77.   XWINDOW (minibuf_window)->last_modified[CURRENT_DISP] = Qzero;
  78.   XWINDOW (minibuf_window)->last_modified[DESIRED_DISP] = Qzero;
  79.   XWINDOW (minibuf_window)->last_modified[CMOTION_DISP] = Qzero;
  80.   XWINDOW (minibuf_window)->last_facechange[CURRENT_DISP] = Qzero;
  81.   XWINDOW (minibuf_window)->last_facechange[DESIRED_DISP] = Qzero;
  82.   XWINDOW (minibuf_window)->last_facechange[CMOTION_DISP] = Qzero;
  83.   Vminibuf_prompt = Felt (unwind_data, Qzero);
  84.   minibuf_level = XINT (Felt (unwind_data, make_number (1)));
  85.   while (CONSP (unwind_data))
  86.     {
  87.       Lisp_Object victim = unwind_data;
  88.       unwind_data = XCDR (unwind_data);
  89.       free_cons (XCONS (victim));
  90.     }
  91.  
  92.   /* If cursor is on the minibuffer line,
  93.      show the user we have exited by putting it in column 0.  */
  94.   frame = Fselected_frame (Qnil);
  95.   if (!noninteractive
  96.       && !NILP (frame)
  97.       && !NILP (XFRAME (frame)->minibuffer_window))
  98.     {
  99.       struct window *w = XWINDOW (XFRAME (frame)->minibuffer_window);
  100.       redisplay_move_cursor (w, 0, 0);
  101.     }
  102.  
  103.   return Qnil;
  104. }
  105.  
  106. DEFUN ("read-minibuffer-internal", 
  107.        Fread_minibuffer_internal, Sread_minibuffer_internal, 
  108.        1, 1, 0,
  109.        "Lowest-level interface to minibuffers.  Don't call this.")
  110.   (prompt)
  111.      Lisp_Object prompt;
  112. {
  113.   /* This function can GC */
  114.   int speccount = specpdl_depth ();
  115.   Lisp_Object val;
  116.  
  117.   CHECK_STRING (prompt, 0);
  118.  
  119.   record_unwind_protect (read_minibuffer_internal_unwind,
  120.                          list2 (Vminibuf_prompt,
  121.                                 make_number (minibuf_level)));
  122.   Vminibuf_prompt = LISP_GETTEXT (prompt);
  123.  
  124.   if (!NILP (Vrun_hooks) && !NILP (Vminibuffer_setup_hook))
  125.     call1 (Vrun_hooks, Qminibuffer_setup_hook);
  126.     
  127.   minibuf_level++;
  128.   clear_echo_area (selected_frame (), Qnil, 0);
  129.  
  130.   val = call_command_loop (Qt);
  131.  
  132.   return (unbind_to (speccount, val));
  133. }
  134.  
  135.  
  136.  
  137. /* Completion hair */
  138.  
  139. /* Compare exactly LEN chars of strings at S1 and S2,
  140.    ignoring case if appropriate.
  141.    Return -1 if strings match,
  142.    else number of chars that match at the beginning.  */
  143.  
  144. /* Note that this function works in Charcounts, unlike most functions.
  145.    This is necessary for many reasons, one of which is that two
  146.    strings may match even if they have different numbers of bytes,
  147.    if IGNORE_CASE is true. */
  148.  
  149. Charcount
  150. scmp_1 (CONST Bufbyte *s1, CONST Bufbyte *s2, Charcount len,
  151.     int ignore_case)
  152. {
  153.   Charcount l = len;
  154.  
  155.   if (ignore_case)
  156.     {
  157.       while (l)
  158.         {
  159.           Bufbyte c1 = DOWNCASE (current_buffer, charptr_to_emchar (s1));
  160.           Bufbyte c2 = DOWNCASE (current_buffer, charptr_to_emchar (s2));
  161.  
  162.           if (c1 == c2)
  163.             {
  164.               l--;
  165.               INC_CHARPTR (s1);
  166.               INC_CHARPTR (s2);
  167.             }
  168.           else
  169.             break;
  170.         }
  171.     }
  172.   else
  173.     {
  174.       while (l && charptr_to_emchar (s1) == charptr_to_emchar (s2))
  175.     {
  176.       l--;
  177.       INC_CHARPTR (s1);
  178.       INC_CHARPTR (s2);
  179.     }
  180.     }
  181.  
  182.   if (l == 0)
  183.     return -1;
  184.   else return len - l;
  185. }
  186.  
  187.  
  188. int
  189. regexp_ignore_completion_p (CONST Bufbyte *nonreloc,
  190.                 Lisp_Object reloc, Bytecount offset,
  191.                 Bytecount length)
  192. {
  193.   /* Ignore this element if it fails to match all the regexps.  */
  194.   if (!NILP (Vcompletion_regexp_list))
  195.     {
  196.       Lisp_Object regexps;
  197.       for (regexps = Vcompletion_regexp_list;
  198.        CONSP (regexps);
  199.        regexps = XCDR (regexps))
  200.     {
  201.       Lisp_Object re = XCAR (regexps);
  202.       if (STRINGP (re)
  203.           && (fast_string_match (re, nonreloc, reloc, offset,
  204.                      length, 0, 0) >= 0))
  205.         return (1);
  206.     }
  207.     }
  208.   return (0);
  209. }
  210.  
  211.  
  212. /* Callers should GCPRO, since this may call eval */
  213. static int
  214. ignore_completion_p (Lisp_Object completion_string,
  215.                      Lisp_Object pred, Lisp_Object completion)
  216. {
  217.   if (regexp_ignore_completion_p (0, completion_string, 0, -1))
  218.     return (1);
  219.   
  220.   /* Ignore this element if there is a predicate
  221.      and the predicate doesn't like it. */
  222.   if (!NILP (pred))
  223.   {
  224.     Lisp_Object tem;
  225.     if (EQ (pred, Qcommandp))
  226.       tem = Fcommandp (completion);
  227.     else
  228.       tem = call1 (pred, completion);
  229.     if (NILP (tem))
  230.       return (1);
  231.   }
  232.   return (0);
  233. }
  234.  
  235.  
  236.  
  237.  
  238. DEFUN ("try-completion", Ftry_completion, Stry_completion, 2, 3, 0,
  239.   "Return common substring of all completions of STRING in ALIST.\n\
  240. Each car of each element of ALIST is tested to see if it begins with STRING.\n\
  241. All that match are compared together; the longest initial sequence\n\
  242. common to all matches is returned as a string.\n\
  243. If there is no match at all, nil is returned.\n\
  244. For an exact match, t is returned.\n\
  245. \n\
  246. ALIST can be an obarray instead of an alist.\n\
  247. Then the print names of all symbols in the obarray are the possible matches.\n\
  248. \n\
  249. ALIST can also be a function to do the completion itself.\n\
  250. It receives three arguments: the values STRING, PREDICATE and nil.\n\
  251. Whatever it returns becomes the value of `try-completion'.\n\
  252. \n\
  253. If optional third argument PREDICATE is non-nil,\n\
  254. it is used to test each possible match.\n\
  255. The match is a candidate only if PREDICATE returns non-nil.\n\
  256. The argument given to PREDICATE is the alist element or the symbol from the obarray.")
  257.   (string, alist, pred)
  258.      Lisp_Object string, alist, pred;
  259. {
  260.   /* This function can GC */
  261.   Lisp_Object bestmatch, tail;
  262.   Charcount bestmatchsize = 0;
  263.   int list;
  264.   int indice = 0;
  265.   int obsize = 0;
  266.   int matchcount = 0;
  267.   Lisp_Object bucket;
  268.   Charcount slength, blength;
  269.  
  270.   CHECK_STRING (string, 0);
  271.  
  272.   if (CONSP (alist))
  273.   {
  274.     Lisp_Object tem = XCAR (alist);
  275.     if (SYMBOLP (tem))          /* lambda, autoload, etc.  Emacs-lisp sucks */
  276.       return call3 (alist, string, pred, Qnil);
  277.     else
  278.       list = 1;
  279.   }
  280.   else if (VECTORP (alist))
  281.     list = 0;
  282.   else if (NILP (alist))
  283.     list = 1;
  284.   else
  285.     return call3 (alist, string, pred, Qnil);
  286.  
  287.   bestmatch = Qnil;
  288.   blength = 0;
  289.   slength = string_char_length (XSTRING (string));
  290.  
  291.   /* If ALIST is not a list, set TAIL just for gc pro.  */
  292.   tail = alist;
  293.   if (!list)
  294.     {
  295.       obsize = vector_length (XVECTOR (alist));
  296.       bucket = vector_data (XVECTOR (alist))[indice];
  297.     }
  298.  
  299.   while (1)
  300.     {
  301.       /* Get the next element of the alist or obarray. */
  302.       /* Exit the loop if the elements are all used up. */
  303.       /* elt gets the alist element or symbol.
  304.      eltstring gets the name to check as a completion. */
  305.       Lisp_Object elt;
  306.       Lisp_Object eltstring;
  307.  
  308.       if (list)
  309.     {
  310.       if (NILP (tail))
  311.         break;
  312.       elt = Fcar (tail);
  313.       eltstring = Fcar (elt);
  314.       tail = Fcdr (tail);
  315.     }
  316.       else
  317.     {
  318.       if (!EQ (bucket, Qzero))
  319.         {
  320.               struct Lisp_Symbol *next = symbol_next (XSYMBOL (bucket));
  321.           elt = bucket;
  322.           eltstring = Fsymbol_name (elt);
  323.               if (next)
  324.         XSETSYMBOL (bucket, next);
  325.           else
  326.         bucket = Qzero;
  327.         }
  328.       else if (++indice >= obsize)
  329.         break;
  330.       else
  331.         {
  332.           bucket = vector_data (XVECTOR (alist))[indice];
  333.           continue;
  334.         }
  335.     }
  336.  
  337.       /* Is this element a possible completion? */
  338.  
  339.       if (STRINGP (eltstring))
  340.     {
  341.       Charcount eltlength = string_char_length (XSTRING (eltstring));
  342.       if (slength <= eltlength
  343.           && (0 > scmp (string_data (XSTRING (eltstring)),
  344.                             string_data (XSTRING (string)),
  345.                             slength)))
  346.         {
  347.               {
  348.                 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  349.                 int loser;
  350.                 GCPRO4 (tail, string, eltstring, bestmatch);
  351.                 loser = ignore_completion_p (eltstring, pred, elt);
  352.                 UNGCPRO;
  353.                 if (loser)      /* reject this one */
  354.                   continue;
  355.               }
  356.  
  357.           /* Update computation of how much all possible
  358.          completions match */
  359.  
  360.           matchcount++;
  361.           if (NILP (bestmatch))
  362.         {
  363.           bestmatch = eltstring;
  364.                   blength = eltlength;
  365.           bestmatchsize = eltlength;
  366.         }
  367.           else
  368.         {
  369.           Charcount compare = min (bestmatchsize, eltlength);
  370.           Charcount matchsize =
  371.             scmp (string_data (XSTRING (bestmatch)),
  372.               string_data (XSTRING (eltstring)),
  373.               compare);
  374.           if (matchsize < 0)
  375.             matchsize = compare;
  376.           if (completion_ignore_case)
  377.             {
  378.               /* If this is an exact match except for case,
  379.              use it as the best match rather than one that is not
  380.              an exact match.  This way, we get the case pattern
  381.              of the actual match.  */
  382.               if ((matchsize == eltlength
  383.                && matchsize < blength)
  384.               ||
  385.               /* If there is more than one exact match ignoring
  386.                  case, and one of them is exact including case,
  387.                  prefer that one.  */
  388.               /* If there is no exact match ignoring case,
  389.                  prefer a match that does not change the case
  390.                  of the input.  */
  391.               ((matchsize == eltlength)
  392.                ==
  393.                (matchsize == blength)
  394.                && 0 > scmp_1 (string_data (XSTRING (eltstring)),
  395.                       string_data (XSTRING (string)),
  396.                       slength, 0)
  397.                && 0 <= scmp_1 (string_data (XSTRING (bestmatch)),
  398.                        string_data (XSTRING (string)), 
  399.                        slength, 0)))
  400.                       {
  401.             bestmatch = eltstring;
  402.                         blength = eltlength;
  403.                       }
  404.             }
  405.           bestmatchsize = matchsize;
  406.         }
  407.         }
  408.     }
  409.     }
  410.  
  411.   if (NILP (bestmatch))
  412.     return Qnil;        /* No completions found */
  413.   /* If we are ignoring case, and there is no exact match,
  414.      and no additional text was supplied,
  415.      don't change the case of what the user typed.  */
  416.   if (completion_ignore_case
  417.       && bestmatchsize == slength
  418.       && blength > bestmatchsize)
  419.     return string;
  420.  
  421.   /* Return t if the supplied string is an exact match (counting case);
  422.      it does not require any change to be made.  */
  423.   if (matchcount == 1
  424.       && bestmatchsize == slength
  425.       && 0 > scmp_1 (string_data (XSTRING (bestmatch)),
  426.              string_data (XSTRING (string)),
  427.              bestmatchsize, 0))
  428.     return Qt;
  429.  
  430.   /* Else extract the part in which all completions agree */
  431.   return Fsubstring (bestmatch, Qzero, make_number (bestmatchsize));
  432. }
  433.  
  434.  
  435. DEFUN ("all-completions", Fall_completions, Sall_completions, 2, 3, 0,
  436.   "Search for partial matches to STRING in ALIST.\n\
  437. Each car of each element of ALIST is tested to see if it begins with STRING.\n\
  438. The value is a list of all the strings from ALIST that match.\n\
  439. ALIST can be an obarray instead of an alist.\n\
  440. Then the print names of all symbols in the obarray are the possible matches.\n\
  441. \n\
  442. ALIST can also be a function to do the completion itself.\n\
  443. It receives three arguments: the values STRING, PREDICATE and t.\n\
  444. Whatever it returns becomes the value of `all-completions'.\n\
  445. \n\
  446. If optional third argument PREDICATE is non-nil,\n\
  447. it is used to test each possible match.\n\
  448. The match is a candidate only if PREDICATE returns non-nil.\n\
  449. The argument given to PREDICATE is the alist element or\n\
  450. the symbol from the obarray.")
  451.   (string, alist, pred)
  452.      Lisp_Object string, alist, pred;
  453. {
  454.   /* This function can GC */
  455.   Lisp_Object tail;
  456.   Lisp_Object allmatches;
  457.   int list;
  458.   int indice = 0;
  459.   int obsize = 0;
  460.   Lisp_Object bucket;
  461.   Charcount slength;
  462.  
  463.   CHECK_STRING (string, 0);
  464.  
  465.   if (CONSP (alist))
  466.   {
  467.     Lisp_Object tem = XCAR (alist);
  468.     if (SYMBOLP (tem))          /* lambda, autoload, etc.  Emacs-lisp sucks */
  469.       return call3 (alist, string, pred, Qt);
  470.     else
  471.       list = 1;
  472.   }
  473.   else if (VECTORP (alist))
  474.     list = 0;
  475.   else if (NILP (alist))
  476.     list = 1;
  477.   else
  478.     return call3 (alist, string, pred, Qt);
  479.  
  480.   allmatches = Qnil;
  481.   slength = string_char_length (XSTRING (string));
  482.  
  483.   /* If ALIST is not a list, set TAIL just for gc pro.  */
  484.   tail = alist;
  485.   if (!list)
  486.     {
  487.       obsize = vector_length (XVECTOR (alist));
  488.       bucket = vector_data (XVECTOR (alist))[indice];
  489.     }
  490.  
  491.   while (1)
  492.     {
  493.       /* Get the next element of the alist or obarray. */
  494.       /* Exit the loop if the elements are all used up. */
  495.       /* elt gets the alist element or symbol.
  496.      eltstring gets the name to check as a completion. */
  497.       Lisp_Object elt;
  498.       Lisp_Object eltstring;
  499.  
  500.       if (list)
  501.     {
  502.       if (NILP (tail))
  503.         break;
  504.       elt = Fcar (tail);
  505.       eltstring = Fcar (elt);
  506.       tail = Fcdr (tail);
  507.     }
  508.       else
  509.     {
  510.       if (!EQ (bucket, Qzero))
  511.         {
  512.               struct Lisp_Symbol *next = symbol_next (XSYMBOL (bucket));
  513.           elt = bucket;
  514.           eltstring = Fsymbol_name (elt);
  515.               if (next)
  516.         XSETSYMBOL (bucket, next);
  517.           else
  518.         bucket = Qzero;
  519.             }
  520.       else if (++indice >= obsize)
  521.         break;
  522.       else
  523.         {
  524.           bucket = vector_data (XVECTOR (alist))[indice];
  525.           continue;
  526.         }
  527.     }
  528.  
  529.       /* Is this element a possible completion? */
  530.  
  531.       if (STRINGP (eltstring) 
  532.           && (slength <= string_char_length (XSTRING (eltstring)))
  533.       /* Reject alternatives that start with space
  534.          unless the input starts with space.  */
  535.       && ((string_char_length (XSTRING (string)) > 0 &&
  536.            string_char (XSTRING (string), 0) == ' ')
  537.           || string_char (XSTRING (eltstring), 0) != ' ')
  538.           && (0 > scmp (string_data (XSTRING (eltstring)),
  539.                         string_data (XSTRING (string)),
  540.                         slength)))
  541.     {
  542.       /* Yes.  Now check whether predicate likes it. */
  543.           struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  544.           int loser;
  545.           GCPRO4 (tail, eltstring, allmatches, string);
  546.           loser = ignore_completion_p (eltstring, pred, elt);
  547.           UNGCPRO;
  548.           if (!loser)
  549.             /* Ok => put it on the list. */
  550.             allmatches = Fcons (eltstring, allmatches);
  551.         }
  552.     }
  553.  
  554.   return Fnreverse (allmatches);
  555. }
  556.  
  557. /* Useless FSFmacs functions */
  558. /* More than useless.  I've nuked minibuf_prompt_width so they won't
  559.    function at all in XEmacs at the moment.  They are used to
  560.    implement some braindamage in FSF which we aren't including. --cet */
  561.  
  562. #if 0
  563. xxDEFUN ("minibuffer-prompt", Fminibuffer_prompt, Sminibuffer_prompt, 0, 0, 0,
  564.   "Return the prompt string of the currently-active minibuffer.\n\
  565. If no minibuffer is active, return nil.")
  566.   ()
  567. {
  568.   return (Fcopy_sequence (Vminibuf_prompt));
  569. }
  570.  
  571. xxDEFUN ("minibuffer-prompt-width", Fminibuffer_prompt_width,
  572.   Sminibuffer_prompt_width, 0, 0, 0,
  573.   "Return the display width of the minibuffer prompt.")
  574.   ()
  575. {
  576.   return (make_number (minibuf_prompt_width));
  577. }
  578. #endif
  579.  
  580.  
  581. /************************************************************************/
  582. /*                              echo area                               */
  583. /************************************************************************/
  584.  
  585. extern int stdout_needs_newline;
  586.  
  587. static Lisp_Object
  588. clear_echo_area_internal (struct frame *f, Lisp_Object label, int from_print,
  589.               int no_restore)
  590. {
  591.   if (!NILP (Ffboundp (Qclear_message)))
  592.     {
  593.       Lisp_Object frame;
  594.  
  595.       XSETFRAME (frame, f);
  596.       return call4 (Qclear_message, label, frame, from_print ? Qt : Qnil, 
  597.             no_restore ? Qt : Qnil);
  598.     }
  599.   else
  600.     {
  601.       write_string_to_stdio_stream (stderr, (Bufbyte *) "\n", 0, 1);
  602.       return Qnil;
  603.     }
  604. }
  605.  
  606. Lisp_Object
  607. clear_echo_area (struct frame *f, Lisp_Object label, int no_restore)
  608. {
  609.   return clear_echo_area_internal (f, label, 0, no_restore);
  610. }
  611.  
  612. Lisp_Object
  613. clear_echo_area_from_print (struct frame *f, Lisp_Object label, int no_restore)
  614. {
  615.   return clear_echo_area_internal (f, label, 1, no_restore);
  616. }
  617.  
  618. void
  619. echo_area_append (struct frame *f, CONST Bufbyte *nonreloc, Lisp_Object reloc,
  620.           Bytecount offset, Bytecount length,
  621.           Lisp_Object label)
  622. {
  623.   Lisp_Object obj;
  624.   struct gcpro gcpro1;
  625.   Lisp_Object frame;
  626.  
  627.   /* some callers pass in a null string as a way of clearing the echo area.
  628.      check for length == 0 now; if this case, neither nonreloc nor reloc
  629.      may be valid.  */
  630.   if (length == 0)
  631.     return;
  632.   
  633.   fixup_internal_substring (nonreloc, reloc, offset, &length);
  634.   
  635.   /* also check it here, in case the string was really blank. */
  636.   if (length == 0)
  637.     return;
  638.  
  639.   if (!NILP (Ffboundp (Qappend_message)))
  640.     {
  641.       if (STRINGP (reloc) && offset == 0 &&
  642.       length == string_length (XSTRING (reloc)))
  643.     obj = reloc;
  644.       else
  645.     {
  646.       if (STRINGP (reloc))
  647.         nonreloc = string_data (XSTRING (reloc));
  648.       obj = make_string (nonreloc + offset, length);
  649.     }
  650.       
  651.       XSETFRAME (frame, f);
  652.       GCPRO1 (obj);
  653.       call4 (Qappend_message, label, obj, frame, 
  654.          EQ (label, Qprint) ? Qt : Qnil);
  655.       UNGCPRO;
  656.     }
  657.   else
  658.     {
  659.       if (STRINGP (reloc))
  660.     nonreloc = string_data (XSTRING (reloc));
  661.       write_string_to_stdio_stream (stderr, nonreloc, offset, length);
  662.     }
  663. }
  664.  
  665. void
  666. echo_area_message (struct frame *f, CONST Bufbyte *nonreloc,
  667.            Lisp_Object reloc, Bytecount offset, Bytecount length,
  668.            Lisp_Object label)
  669. {
  670.   clear_echo_area (f, label, 1);
  671.   echo_area_append (f, nonreloc, reloc, offset, length, label);
  672. }
  673.  
  674. int
  675. echo_area_active (struct frame *f)
  676. {
  677.   /* By definition, the echo area is active if the echo-area buffer
  678.      is not empty.  No need to call Lisp code. (Anyway, this function
  679.      is called from redisplay.) */
  680.   return (BUF_BEGV (XBUFFER (Vecho_area_buffer)) !=
  681.       BUF_ZV (XBUFFER (Vecho_area_buffer)));
  682. }
  683.  
  684. Lisp_Object
  685. echo_area_status (struct frame *f)
  686. {
  687.   if (!NILP (Ffboundp (Qcurrent_message_label)))
  688.     {
  689.       Lisp_Object frame;
  690.  
  691.       XSETFRAME (frame, f);
  692.       return (call1 (Qcurrent_message_label, frame));
  693.     }
  694.   else
  695.     return stdout_needs_newline ? Qmessage : Qnil;
  696. }
  697.  
  698. Lisp_Object
  699. echo_area_contents (struct frame *f)
  700. {
  701.   /* See above.  By definition, the contents of the echo-area buffer
  702.      are the contents of the echo area. */
  703.   return Fbuffer_substring (Qnil, Qnil, Vecho_area_buffer);
  704. }
  705.  
  706. /* Dump an informative message to the echo area.  This function takes a
  707.    string in internal format. */
  708. void
  709. message_internal (CONST Bufbyte *nonreloc, Lisp_Object reloc,
  710.           Bytecount offset, Bytecount length)
  711. {
  712.   /* This can GC! */
  713.   if (NILP (Vexecuting_macro))
  714.     echo_area_message (selected_frame (), nonreloc, reloc, offset, length,
  715.                Qmessage);
  716. }
  717.  
  718. /* The next three functions are interfaces to message_internal() that
  719.    take strings in external format.  message() does I18N3 translating
  720.    on the format string; message_no_translate() does not. */
  721.  
  722. static void
  723. message_1 (CONST char *fmt, va_list args)
  724. {
  725.   if (fmt)
  726.     {
  727.       struct gcpro gcpro1;
  728.       /* message_internal() might GC, e.g. if there are after-change-hooks
  729.      on the echo area buffer */
  730.       Lisp_Object obj = emacs_doprnt_string_va ((CONST Bufbyte *) fmt, Qnil,
  731.                         -1, args);
  732.       GCPRO1 (obj);
  733.       message_internal (0, obj, 0, -1);
  734.       UNGCPRO;
  735.     }
  736.   else
  737.     message_internal (0, Qnil, 0, 0);
  738. }
  739.  
  740. void
  741. message (CONST char *fmt, ...)
  742. {
  743.   /* I think it's OK to pass the data of Lisp strings as arguments to
  744.      this function.  No GC'ing will occur until the data has already
  745.      been copied. */
  746.   va_list args;
  747.  
  748.   va_start (args, fmt);
  749.   if (fmt)
  750.     fmt = GETTEXT (fmt);
  751.   message_1 (fmt, args);
  752.   va_end (args);
  753. }
  754.  
  755. void
  756. message_no_translate (CONST char *fmt, ...)
  757. {
  758.   /* I think it's OK to pass the data of Lisp strings as arguments to
  759.      this function.  No GC'ing will occur until the data has already
  760.      been copied. */
  761.   va_list args;
  762.  
  763.   va_start (args, fmt);
  764.   message_1 (fmt, args);
  765.   va_end (args);
  766. }
  767.  
  768.  
  769. /************************************************************************/
  770. /*                            initialization                            */
  771. /************************************************************************/
  772.  
  773. void
  774. syms_of_minibuf (void)
  775. {
  776.   defsymbol (&Qminibuffer_setup_hook, "minibuffer-setup-hook");
  777.  
  778.   defsymbol (&Qcompletion_ignore_case, "completion-ignore-case");
  779.  
  780.   defsubr (&Sminibuffer_depth);
  781. #if 0
  782.   defsubr (&Sminibuffer_prompt);
  783.   defsubr (&Sminibuffer_prompt_width);
  784. #endif
  785.  
  786.   defsubr (&Sread_minibuffer_internal);
  787.  
  788.   defsubr (&Stry_completion);
  789.   defsubr (&Sall_completions);
  790.  
  791.   defsymbol (&Qappend_message, "append-message");
  792.   defsymbol (&Qclear_message, "clear-message");
  793.   defsymbol (&Qdisplay_message, "display-message");
  794.   defsymbol (&Qcurrent_message_label, "current-message-label");
  795. }
  796.  
  797. void
  798. vars_of_minibuf (void)
  799. {
  800.   minibuf_level = 0;
  801.  
  802.   staticpro (&Vminibuf_prompt);
  803.   Vminibuf_prompt = Qnil;
  804.  
  805.   DEFVAR_LISP ("minibuffer-setup-hook", &Vminibuffer_setup_hook,
  806.     "Normal hook run just after entry to minibuffer.");
  807.   Vminibuffer_setup_hook = Qnil;
  808.  
  809.   DEFVAR_BOOL ("completion-ignore-case", &completion_ignore_case,
  810.     "Non-nil means don't consider case significant in completion.");
  811.   completion_ignore_case = 0;
  812.  
  813.   /* Worthless doc string */
  814.   DEFVAR_LISP ("completion-regexp-list", &Vcompletion_regexp_list,
  815.     "List of regexps that should restrict possible completions.");
  816.   Vcompletion_regexp_list = Qnil;
  817. }
  818.  
  819. void
  820. complex_vars_of_minibuf (void)
  821. {
  822.   /* This function can GC */
  823. #ifdef I18N3
  824. ]  /* #### This needs to be fixed up so that the gettext() gets called
  825.      at runtime instead of at load time. */
  826. #endif
  827.   Vminibuffer_zero
  828.     = Fget_buffer_create
  829.       (Fpurecopy (build_string (DEFER_GETTEXT (" *Minibuf-0*"))));
  830.   Vecho_area_buffer
  831.     = Fget_buffer_create
  832.       (Fpurecopy (build_string (DEFER_GETTEXT (" *Echo Area*"))));
  833. }
  834.